package org.misty.seckill.util;

import org.springframework.util.DigestUtils;

public class MD5Util {
  private static final String SALT = "1a2b3c4d";

  public static String md5(String src) {
    return DigestUtils.md5DigestAsHex(src.getBytes());
  }

  public static String inputToFormPass(String inputPass) {
    String buf = inputPass.substring(0, 3) +
        SALT.substring(0, 4) +
        inputPass.substring(3, inputPass.length()) +
        SALT.substring(3, 8);
    return md5(buf);
  }

  public static String formToDbPass(String formPass, String salt) {
    var n = salt.length() / 2;
    var buf = new StringBuilder()
        .append(SALT, 0, n)
        .append(formPass)
        .append(SALT, n, salt.length());
    return md5(buf.toString());
  }

  public static String inputToDbPass(String inputPass, String salt) {
    var first = inputToFormPass(inputPass);
    return formToDbPass(first, salt);
  }

  public static void main(String[] args) {
    String a;
    System.out.println(a = inputToFormPass("123456"));
    System.out.println(formToDbPass(a, "a1b2c3d4"));
    System.out.println(inputToDbPass("123456", "a1b2c3d4"));
  }
}
